const app = getApp();
const { processListImages } = require('../../utils/image')
const request = require('../../utils/request.js');
Page({
  data: {
    list: [],
    page: 1,
    pageSize: 10,
    hasMore: true,
    loading: false,
    searchKeyword: ''
  },

  onLoad() {
    this.loadList();
  },

  onShow() {
    if (app.globalData.needRefresh) {
      this.setData({
        page: 1,
        hasMore: true,
        list: []
      })
      // 重新请求列表数据
      setTimeout(() => {
        this.loadList();
      },1500)
      // 重置标志位
      app.globalData.needRefresh = false;
    }
  },

  // 加载列表
  loadList() {
    if (this.data.loading || !this.data.hasMore) return;
    this.setData({ loading: true });
    // 模拟加载数据
    return new Promise((resolve, reject) => {
      request.request({
        url: `${app.globalData.baseUrl}/api/agricultural-knowledge/residues`,
        method: 'GET',
        data: {
          page: this.data.page,
          size: this.data.pageSize,
        },
        header: {
          'Authorization': `Bearer ${app.getGlobalToken()}`
        },
        success: (res) => {
          const { list, total } = res.data.data
          // 使用工具函数处理图片URL
          const processedList = processListImages(list)
          this.setData({
            list: [...this.data.list, ...processedList],
            hasMore: this.data.list.length + processedList.length < total,
            loading: false
          })
          resolve()
        },
        fail: reject
      })
    })
  },

  // 搜索
  onSearch(e) {
    this.setData({
      searchKeyword: e.detail.value,
      list: [],
      page: 1,
      hasMore: true
    });
    this.loadList();
  },

  // 跳转到发布页面
  goToPublish() {
    wx.navigateTo({
      url: '/pages/agricultural-residue/publish/publish'
    });
  },

  // 跳转到详情页面
  goToDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/agricultural-residue/detail/detail?id=${id}`
    });
  },

  // 编辑
  editItem(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/agricultural-residue/publish/publish?id=${id}`
    });
  },

  // 删除
  deleteItem(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除这条农余信息吗？',
      success: (res) => {
        if (res.confirm) {
          // 模拟删除
          request.request({
            url: `${app.globalData.baseUrl}/api/agricultural-knowledge/residues/${id}`,
            method: 'delete',
            header: {
              'Authorization': `Bearer ${app.getGlobalToken()}`
            },
            success: () => {
              wx.showToast({
                title: '已删除',
                icon: 'success'
              })
              this.setData({
                list: [],
                page: 1,
                hasMore: true
              })
              this.loadList()
            }
          })
        }
      }
    });
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.setData({
      list: [],
      page: 1,
      hasMore: true
    });
    this.loadList();
    wx.stopPullDownRefresh();
  },

  // 上拉加载更多
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    })
    this.loadList()
  }
}); 